﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DA_Company
/// </summary>
public class DA_Company
{
    AuditBillingEntities objABEnt;
    public DA_Company()
    {
        //
        // TODO: Add constructor logic here
        //
    }
 
    public List<PLCompany> DA_fnGetCompanyList(PLCompany pObjComp)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from u in objABEnt.MstCompanies
                          where (pObjComp.SearchIsActive == null || u.IsActive == pObjComp.SearchIsActive) &&
                                (pObjComp.CompanyID == null || pObjComp.CompanyID == u.MstCompanyID) &&
                                (string.IsNullOrEmpty(pObjComp.Name) || u.Name.ToLower().Contains(pObjComp.Name.ToLower()))
                          orderby u.Name
                          select new PLCompany()
                          {
                              CompanyID = u.MstCompanyID,
                              Name = u.Name,
                              Address1 = u.Address1,
                              Address2 = u.Address2,
                              City = u.City,
                              PinCode = u.PinCode,
                              Phone = u.Phone,
                              Email = u.Email,
                              Fax = u.Fax,
                              PAN = u.PAN,
                              ServiceTaxNo = u.ServiceTaxNo,
                              IsActive = u.IsActive,
                              BankName = u.BankName,
                              BankAccountName = u.BankAccountName,
                              BankAccountNo = u.BankAccountNo,
                              BankBranch = u.BankBranch,
                              BankIFSCCode = u.BankIFSCCode,
                              BankIMCR = u.BankIMCR,
                              TermsAndCondition = u.TermsAndCondition
                          });
            return varRes.ToList();
        }
    }

    public List<PLCompany> DA_fnGetCompanyNameList(PLCompany pObjComp)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from u in objABEnt.MstCompanies
                          where (pObjComp.SearchIsActive == null || u.IsActive == pObjComp.SearchIsActive) &&
                                (pObjComp.CompanyID == null || pObjComp.CompanyID == u.MstCompanyID)
                          orderby u.Name
                          select new PLCompany()
                          {
                              CompanyID = u.MstCompanyID,
                              Name = u.Name,
                              IsActive = u.IsActive
                          });
            return varRes.ToList();
        }
    }

    public int DA_InsertUpdateCompany(PLCompany pObjComp)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            MstCompany objComp = new MstCompany();
            if (pObjComp.CompanyID.GetValueOrDefault() != 0)
            {
                objComp = (from u in objABEnt.MstCompanies
                            where u.MstCompanyID == pObjComp.CompanyID
                           select u).ToList().FirstOrDefault();
            }

            objComp.Name = pObjComp.Name;
            objComp.Address1 = pObjComp.Address1;
            objComp.Address2 = pObjComp.Address2;
            objComp.City = pObjComp.City;
            objComp.PinCode = pObjComp.PinCode;
            objComp.Phone = pObjComp.Phone;
            objComp.Email = pObjComp.Email;
            objComp.Fax = pObjComp.Fax;
            objComp.PAN = pObjComp.PAN;
            objComp.ServiceTaxNo = pObjComp.ServiceTaxNo;
            objComp.IsActive = pObjComp.IsActive;
            objComp.BankName = pObjComp.BankName;
            objComp.BankAccountName = pObjComp.BankAccountName;
            objComp.BankAccountNo = pObjComp.BankAccountNo;
            objComp.BankBranch = pObjComp.BankBranch;
            objComp.BankIFSCCode = pObjComp.BankIFSCCode;
            objComp.BankIMCR = pObjComp.BankIMCR;
            objComp.TermsAndCondition = pObjComp.TermsAndCondition;



            if (pObjComp.CompanyID.GetValueOrDefault() != 0)
            {
                objComp.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                objComp.ModifiedOn = DateTime.Now;
            }
            else
            {
                objComp.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                objComp.CreatedOn = DateTime.Now;
                objABEnt.MstCompanies.Add(objComp);
            }
            objABEnt.SaveChanges();
            return objComp.MstCompanyID;
        }
    }
}